package com.kx.center.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

public class IOHelper {
	
	private static final String DEFAULT_CHARSET = "GB2312";

	public static List<String> getLines(String filepath) throws IOException{
		return getLines(filepath, DEFAULT_CHARSET);
	}
	
	public static List<String> getLines(String filepath, String encoding) throws IOException{
		return getLines(new File(filepath), encoding);
	}
	
	public static List<String> getLines(File file) throws IOException{
		return getLines(file, DEFAULT_CHARSET);
	}
	
	public static List<String> getLines(File file, String encoding) throws IOException{
		
		//声明一个保存文件每行数据的变量
		List<String> contents = new ArrayList<String>();
		
		if(!file.exists()){
			return contents;
		}

		FileInputStream fis = new FileInputStream(file);
		InputStreamReader isr = new InputStreamReader(fis, encoding);
		BufferedReader reader = new BufferedReader(isr);
		
		String line = reader.readLine(); 
		
		while(line != null)//获取文件内容
		{
			if(StringUtils.isBlank(line)) continue;
			contents.add(line);
			line = reader.readLine();
		}
		reader.close();
		isr.close();
		fis.close();
		
		return contents;
	}
	
	public static void writeFile(String filepath, Set<?> contents) throws IOException{
		FileOutputStream fos = new FileOutputStream(filepath);
		OutputStreamWriter osw = new OutputStreamWriter(fos, DEFAULT_CHARSET);
		BufferedWriter writer = new BufferedWriter(osw);
		
		for(Object obj : contents){
			writer.write(obj.toString());
			writer.newLine();
			writer.flush();
		}
		writer.close();
		osw.close();
		fos.close();
	}
	
	public static void writeFile(String filepath, Set<?> contents, String tag, boolean append) throws IOException{
		
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filepath, append)));
		
		for(Object obj : contents){
			out.println(obj.toString()+","+tag);
		}
		out.flush();
		out.close();
	}
	
	public static void writeFile(String filepath, ArrayList<String> contents) throws IOException{
		FileOutputStream fos = new FileOutputStream(filepath);
		OutputStreamWriter osw = new OutputStreamWriter(fos, DEFAULT_CHARSET);
		BufferedWriter writer = new BufferedWriter(osw);
		
		for(String str : contents){
			writer.write(str);
			writer.newLine();
			writer.flush();
		}
		writer.close();
		osw.close();
		fos.close();
	}
	
	public static void writeFile(String filepath, ArrayList<String> contents, String encode, Boolean flag) throws IOException{
		FileOutputStream fos = new FileOutputStream(filepath);
		OutputStreamWriter osw = new OutputStreamWriter(fos, encode);
		BufferedWriter writer = new BufferedWriter(osw);
		
		for(int i=0; i<contents.size(); i++){
			String line = contents.get(i);
			if(flag)
				writer.write((i + 1) + "," + line + "\n");
			else
				writer.write(line + "\n");
			writer.flush();
		}
		writer.close();
		osw.close();
		fos.close();
	}

	public static void writeFile(String filepath, ArrayList<String> contents, String encode) throws IOException{
		writeFile(filepath, contents, encode, false);
	}
	
	public static void delete(String filepath){
		File f = new File(filepath);
		if(f.exists()){
			f.delete();
		}
	}
}
